<template>
    <div>
      <el-card v-for="message in messages" :key="message.id" class="message-card">
        <div class="message-header">
          <h3>{{ message.title }}</h3>
          <span>{{ formatDate(message.sentAt) }}</span>
        </div>
        <div class="message-content">
          <p>{{ message.messageContent }}</p>
        </div>
      </el-card>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref, onMounted } from 'vue';
  import { ElCard } from 'element-plus';
  import { SystemMessage } from '@/interface/type';
import axios from 'axios';
import { useUserStore } from '@/stores/user';
import { ElMessage } from 'element-plus';
const userStore = useUserStore();
  // 假设从后端获取数据的函数
  const fetchSystemMessages = async ()  => {
    axios.get('/api/messages/user/'+userStore.userInfo.id).then(res=>{
      console.log(res);
      
        if(res.data.code !== 200){
          return ElMessage.error(res.data.message)
        }
        messages.value = res.data.data
        ElMessage.success("获取系统信息成功")
    }).catch(err=>{
        ElMessage.error("获取系统信息失败",err)
    })
 
      // 可以添加更多测试数据...
  
  };
  
  // 定义数据类型
  const messages = ref<SystemMessage[]>([]);
  
  // 格式化日期函数
  const formatDate = (dateString: string) => {
    const date = new Date(dateString);
    return date.toLocaleString();
  };
  
  // 在组件加载时获取消息
  onMounted(async () => {
    await fetchSystemMessages();
  });
  </script>
  
  <style scoped>
  .message-card {
    margin-bottom: 20px;
    border: 1px solid #ebeef5;
  }
  
  .message-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .message-header h3 {
    font-size: 16px;
    font-weight: bold;
  }
  
  .message-header span {
    color: #909399;
    font-size: 14px;
  }
  
  .message-content {
    margin-top: 10px;
    font-size: 14px;
    color: #606266;
  }
  </style>
  